/*  Settings.Java - Settings for Intelligent Battery Monitor 
 *  
        Copyright (C) 2009 Devendra Laulkar

        This program is free software: you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation version 2 of the License. 

        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.

        You should have received a copy of the GNU General Public License
        along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package com.intellBatt;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TimePicker;

public class Settings extends Activity {
	public static final String KEY_ALERT_LEVEL = "lvl";
	public static final String KEY_START_HOUR = "shr";
	public static final String KEY_START_MIN = "smn";
	public static final String KEY_END_HOUR = "ehr";
	public static final String KEY_END_MIN = "emn";
	
	public static final int DEFAULT_START_HOUR = 9;
	public static final int DEFAULT_START_MIN = 0;
	public static final int DEFAULT_END_HOUR = 21;
	public static final int DEFAULT_END_MIN = 0;
	
	private RadioGroup alertLevel;
	private EditText alertLevelText;
	private TimePicker startTime, endTime;
	
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		startTime = (TimePicker)findViewById(R.id.startTime);
		endTime = (TimePicker)findViewById(R.id.endTime);
		
		alertLevelText = (EditText)findViewById(R.id.levelText);
		alertLevelText.setSingleLine(true);

		alertLevel = (RadioGroup) findViewById(R.id.alertLevel);


		//alertLevel.check(R.id.level40);

		alertLevel.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
		{

			public void onCheckedChanged(RadioGroup group, int checkedId) 
			{

				Log.i("Check changed, the id is ", Integer.toString(checkedId));
				switch(checkedId)
				{
				case R.id.level60:
					alertLevelText.setText("60%");
					alertLevelText.setEnabled(false);
					break;
				case R.id.level40:
					alertLevelText.setText("40%");
					alertLevelText.setEnabled(false);
					break;
				case R.id.level20:
					alertLevelText.setText("20%");
					alertLevelText.setEnabled(false);
					break;

				case R.id.levelCustom:
					alertLevelText.setEnabled(true);
					break;
				}
			}

		});

		loadSettings();
	}

	private void loadSettings() {
		SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
		int level = settings.getInt(KEY_ALERT_LEVEL, 40);
		alertLevelText.setEnabled(false);
		switch(level)
		{
		case 60:
			alertLevel.check(R.id.level60);
			alertLevelText.setText("60");
			break;
		case 40:
			alertLevel.check(R.id.level40);
			alertLevelText.setText("40");
			break;
		case 20:
			alertLevel.check(R.id.level20);
			alertLevelText.setText("20");
			break;
		default:
			alertLevel.check(R.id.levelCustom);
		alertLevelText.setText("" + level);
		alertLevelText.setEnabled(true);
		}
		//xmlPath = settings.getString(XML_WEBPATH, getResources().getText(R.string.default_xml_webpath).toString());

		startTime.setCurrentHour(settings.getInt(KEY_START_HOUR, 9));
		startTime.setCurrentMinute(settings.getInt(KEY_START_MIN, 0));
		endTime.setCurrentHour(settings.getInt(KEY_END_HOUR, 21));
		endTime.setCurrentMinute(settings.getInt(KEY_END_MIN, 0));
	}

	public void onStop()
	{
		super.onStop();
		SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
		Editor e = settings.edit();
		int op;
		try{
			op = Integer.parseInt(alertLevelText.getText().toString());
		}
		catch (Exception exp)
		{
			op = 40;
		}

		if(op < 10)
		{
			op = 10;
		}
		if(op > 100)
		{
			op = 100;
		}
		e.putInt(KEY_ALERT_LEVEL, op);
		e.putInt(KEY_START_HOUR, startTime.getCurrentHour());
		e.putInt(KEY_START_MIN, startTime.getCurrentMinute());
		e.putInt(KEY_END_HOUR, endTime.getCurrentHour());
		e.putInt(KEY_END_MIN, endTime.getCurrentMinute());
		
		e.commit();
		//Log.i("Settings", "onstop");
		stopService(new Intent("com.IntelBat.services.BATT"));
		stopService(new Intent("com.IntelBat.services.LOCATION"));
		startService(new Intent("com.IntelBat.services.BATT"));
	}
}